package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/22
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {

        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findByid(Long userID) {
        UserInfo userInfo = userInfoMapper.selectById(userID);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfo) {
        QueryWrapper qw=new QueryWrapper();
        qw.in("id",userIds);

        if(userInfo!=null&&userInfo.getAge() != null){
            qw.lt("age",userInfo.getAge());
        }
        if(userInfo!=null&&!StringUtils.isEmpty(userInfo.getGender())){
            qw.eq("gender",userInfo.getGender());
        }
        if(userInfo!=null&&!StringUtils.isEmpty(userInfo.getNickname())){
            qw.like("nickname",userInfo.getNickname());
        }

        List<UserInfo> list = userInfoMapper.selectList(qw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    /**
     * 分页查询
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pagesize) {

        return userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);

    }

}
